<template>
  <div class="login">
    <h2>登录</h2>
    <form @submit.prevent="login">
      <label for="username">用户名:</label>
      <input type="text" id="username" v-model="username" placeholder="请输入用户名">
      <label for="password">密码:</label>
      <input type="password" id="password" v-model="password" placeholder="请输入密码">
      <button type="submit">登录</button>
    </form>
  </div>
</template>

<script>
import { mapMutations } from 'vuex';

export default {
  name: 'Login',
  data() {
    return {
      username: '',
      password: ''
    };
  },
  methods: {
    login() {
      if (this.username === 'admin' && this.password === '123456') {
        this.$store.commit('setLoggedIn', true);
        this.$router.push('/home');
      } else {
        alert('用户名或密码错误，请重新输入！');
      }
    },
    ...mapMutations(['setLoggedIn'])
  }
};
</script>

<style scoped>
.login {
  text-align: center;
  padding: 50px;
}

form {
  display: flex;
  flex-direction: column;
  align-items: center;
}

label {
  margin-top: 10px;
}

input {
  width: 200px;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

button {
  width: 220px;
  padding: 10px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  margin-top: 20px;
}
</style>